package com.example;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;

public class DataInitializer {
    private static final String BASE_URL = "http://localhost:8081/api";
    
    public static void main(String[] args) {
        try {
            // 创建用户数据
            createUser("张三", 1, "13800138000", "zhangsan@example.com", "技术部");
            createUser("李四", 2, "13900139000", "lisi@example.com", "市场部");
            createUser("王五", 1, "13700137000", "wangwu@example.com", "人事部");
            
            System.out.println("用户数据初始化完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void createUser(String name, int gender, String mobile, String email, String deptName) throws Exception {
        // 手动构建JSON数据
        String jsonInputString = "{\"userName\":\"" + name + "\",\"userGender\":" + gender + ",\"userMobile\":\"" + mobile + "\",\"userEmail\":\"" + email + "\",\"userDeptName\":\"" + deptName + "\",\"userAvatar\":\"https://via.placeholder.com/150\",\"userStatus\":true,\"userBirthday\":\"1990-01-01\",\"userJoinAt\":\"2020-01-01\"}";
        
        // 发送POST请求到/save接口
        String url = BASE_URL + "/user/save";
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json; utf-8");
        conn.setRequestProperty("Accept", "application/json");
        conn.setDoOutput(true);
        
        try (OutputStream os = conn.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }
        
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("用户 " + name + " 创建成功");
        } else {
            System.out.println("用户 " + name + " 创建失败，响应码: " + responseCode);
        }
    }
}